<html>
<head><meta charset="utf-8"><title>const impl bounds · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html">const impl bounds</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="231075763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231075763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hannah E <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231075763">(Mar 19 2021 at 19:13)</a>:</h4>
<p>Hello :)</p>
<p>I'm playing with the const generics features on nightly, and am not sure about the syntax. This:</p>
<div class="codehilite"><pre><span></span><code>pub trait A&lt;T&gt; { fn a(self) -&gt; T; }
pub trait B&lt;T&gt; { fn b(T) -&gt; Self; }

impl&lt;T, U: B&lt;T&gt;&gt; const A&lt;U&gt; for T { fn a(self) -&gt; U { B::b(self) } }
</code></pre></div>
<p>fails. The error suggests that B::b  is not a const function. Is there a way to provide this impl only when that is the case?</p>



<a name="231075863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231075863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hannah E <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231075863">(Mar 19 2021 at 19:14)</a>:</h4>
<p>Sorry if I messed it up, its my first time asking here. I've tried it in the playground, and it also fails there: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=a29cdfaa66257a40bc67043fe3f01741">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=a29cdfaa66257a40bc67043fe3f01741</a></p>



<a name="231075912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231075912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hannah E <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231075912">(Mar 19 2021 at 19:14)</a>:</h4>
<p>Is there maybe a channel for beginner questions here?</p>



<a name="231076288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231076288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231076288">(Mar 19 2021 at 19:17)</a>:</h4>
<p><code>impl const Trait</code> is a different feature from const generics, what exactly are you trying to do?</p>



<a name="231076422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231076422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hannah E <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231076422">(Mar 19 2021 at 19:18)</a>:</h4>
<p>I want to provide that trait impl of <code>const A&lt;U&gt; for T</code></p>



<a name="231076592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231076592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hannah E <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231076592">(Mar 19 2021 at 19:20)</a>:</h4>
<p><code>A</code> and <code>B</code> are like <code>From</code> and <code>Into</code>. I really like that I can provide <code>const From</code> impls for my types, but I always forget that then I can only use <code>From::from</code> in <code>const</code> functions, and that <code>.into()</code> does not work because the blanket impls of it for types that implement <code>From</code> are not <code>const</code> :(</p>



<a name="231076665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231076665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hannah E <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231076665">(Mar 19 2021 at 19:20)</a>:</h4>
<p>I tried to provide a specialization of  the blanket <code>Into</code> impl but unfortunately the one in <code>std</code> is not <code>default</code> :(</p>



<a name="231076754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231076754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231076754">(Mar 19 2021 at 19:21)</a>:</h4>
<p>hmm, not sure why this doesn't work, I believe it is intended to work though</p>



<a name="231076805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231076805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231076805">(Mar 19 2021 at 19:21)</a>:</h4>
<p>we're pretty far away from marking impls as <code>const</code> in libstd, the feature is still very experimental</p>



<a name="231076897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231076897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hannah E <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231076897">(Mar 19 2021 at 19:22)</a>:</h4>
<p>When using bounds in const functions, the impls are automatically required to be const. Maybe that is different for trait impls?</p>



<a name="231076988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231076988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231076988">(Mar 19 2021 at 19:23)</a>:</h4>
<p>could be, yeah</p>



<a name="231076995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231076995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hannah E <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231076995">(Mar 19 2021 at 19:23)</a>:</h4>
<p>I tried <code>impl&lt;T, U: const B&lt;T&gt;&gt; const A&lt;U&gt;...</code> but <code>U: const B&lt;T&gt;</code> is not allowed. From reading the docs it does not appear necessary.</p>



<a name="231077081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231077081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231077081">(Mar 19 2021 at 19:23)</a>:</h4>
<p>yeah, the intention is that all bounds have to be const when something is used from a const context unless you opt out with <code>?const</code></p>



<a name="231077139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231077139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hannah E <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231077139">(Mar 19 2021 at 19:24)</a>:</h4>
<p>For functions at least, if one doesn't want to require a <code>const impl</code> in the bounds, one needs to write <code>U: ?const B&lt;T&gt;</code></p>



<a name="231077199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231077199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231077199">(Mar 19 2021 at 19:24)</a>:</h4>
<p>yeah exactly, I think this is supposed to work the same on impls</p>



<a name="231077219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231077219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231077219">(Mar 19 2021 at 19:24)</a>:</h4>
<p>but it might not</p>



<a name="231077304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231077304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hannah E <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231077304">(Mar 19 2021 at 19:25)</a>:</h4>
<p>I hope I am not doing something wrong.<br>
I've tried to find a workaround, but all my ideas were bad.</p>



<a name="231077523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231077523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231077523">(Mar 19 2021 at 19:27)</a>:</h4>
<p>we should probably give more information when that error occurs, this could help narrow down the issue</p>



<a name="231077777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231077777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hannah E <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231077777">(Mar 19 2021 at 19:29)</a>:</h4>
<p>Is there another place I could ask for help ?</p>



<a name="231081543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231081543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231081543">(Mar 19 2021 at 19:59)</a>:</h4>
<p>maybe over at <a class="stream" data-stream-id="146212" href="/#narrow/stream/146212-t-compiler.2Fconst-eval">#t-compiler/const-eval</a> someone more familiar with how this is supposed to work can help</p>



<a name="231082934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231082934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hannah E <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231082934">(Mar 19 2021 at 20:10)</a>:</h4>
<p>Hello :) I have a question about const impl bounds. Is it possible to move this thread <a href="#narrow/stream/122651-general/topic/const.20impl.20bounds">https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/const.20impl.20bounds</a> to this stream?</p>



<a name="231144667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231144667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231144667">(Mar 20 2021 at 12:53)</a>:</h4>
<p>This topic was moved here from <a class="stream-topic" data-stream-id="122651" href="/#narrow/stream/122651-general/topic/const.20impl.20bounds">#general &gt; const impl bounds</a> by <span class="user-mention silent" data-user-id="124288">oli</span></p>



<a name="231145036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231145036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231145036">(Mar 20 2021 at 13:00)</a>:</h4>
<p>yea, this should compile, looks like "just an oversight" in the appropriate logic. Here's a minimized version:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">A</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">a</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">b</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span>: <span class="nc">B</span><span class="o">&gt;</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">A</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">a</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">U</span>::<span class="n">b</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="231145080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20impl%20bounds/near/231145080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20impl.20bounds.html#231145080">(Mar 20 2021 at 13:01)</a>:</h4>
<p>If you want, you can open an issue about it and create a PR that adds this test to our test suite as a failure test so we'll fix it in the future</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>